package com.organization

import grails.test.*

class EmployeeTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testConstraints() {
        def dep = new Department(name: "BU WAC")
        mockDomain(Department, [ dep ])
        def proj1 = new Project(name: "Proj1",startDate: new Date())
        def proj2 = new Project(name: "Proj2",startDate: new Date())
        mockDomain(Project, [ proj1, proj2 ])
        dep.addToProjects(proj1)
        dep.addToProjects(proj2)
        assertEquals 2,Project.findAll().size()

        //try to add not valid employee
        mockDomain(Employee)
        def testEmpl = new Employee(
                lastName: "I",
                extensionPhone: "123",
                homePhone: "123",
                mobilePhone: "+790512",
                email: "email@email",
                addressStreetCity: "------------------> more then 32 chars <------------------------------------",
                gender: "Male"

        )
        assertFalse testEmpl.validate()
        assertEquals "size", testEmpl.errors["lastName"]
        assertEquals "nullable", testEmpl.errors["firstName"]
        assertEquals "matches", testEmpl.errors["extensionPhone"]
        assertEquals "matches", testEmpl.errors["homePhone"]
        assertEquals "matches", testEmpl.errors["mobilePhone"]
        assertEquals "email", testEmpl.errors["email"]
        assertEquals "size", testEmpl.errors["addressStreetCity"]
        assertEquals "inList", testEmpl.errors["gender"]

        //try to add valid employee
        testEmpl = new Employee(project: proj1, lastName: "Ivanov", firstName: "Ivan");
        assertNotNull "can't save employee", testEmpl.save()
        assertEquals 1,Employee.findAll().size()
    }

}
